package com.aletto.general_store.presenters;

import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.AuctionGoods;
import com.aletto.general_store.data.model.AuctionPageGoods;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.PageResponse;
import com.aletto.general_store.data.model.Response;
import com.aletto.general_store.presenters.core.SimplePageTemplate;
import com.aletto.general_store.ui.adapters.AuctionGoodsAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.sisyphus.spinach.core.INetPageTask;
import com.sisyphus.spinach.core.model.IPageResponse;
import com.sisyphus.spinach.core.model.IResponse;
import com.sisyphus.spinach.network.presenters.NetPageTask;
import com.sisyphus.spinach.ui.configs.impl.DefaultPageTemplate;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Function;

public class AuctionGoodsPageTemplate  extends SimplePageTemplate<AuctionGoods> {

    private int type;
    private String keyword;

    public AuctionGoodsPageTemplate(RecyclerView recyclerView, SmartRefreshLayout refreshLayout) {
        super(recyclerView, refreshLayout);
    }

    @Override
    public BaseQuickAdapter<AuctionGoods, BaseViewHolder> adapter(RecyclerView recyclerView) {
        return new AuctionGoodsAdapter();
    }

    public void setParams(int type, String keyword) {
        this.type = type;
        this.keyword = keyword;
    }

    @Override
    public INetPageTask.Presenter<AuctionGoods, INetPageTask.View<AuctionGoods>> presenter() {
        return new NetPageTask<>() {
            @Override
            public Observable<? extends IResponse<? extends IPageResponse<AuctionGoods>>> execute(int page) {
                return DataRepository.getInstance().myGoodsList(page, PageResponse.SIZE, type, keyword)
                        .map((Function<Response<AuctionPageGoods>, IResponse<? extends IPageResponse<AuctionGoods>>>) response -> {
                            AuctionPageGoods data = response.getData();
                            AuctionPageGoods.DataDTO dataDTO = data.getData();
                            return new Response<>(dataDTO, response.getCode(), response.getMessage());
                        });
            }
        };
    }

}
